<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
        方法二：滑动窗口，一般来说，子数组问题，都可以用滑动窗口来解决
        定义窗口左边界left=0 
        遍历nums数组，如果窗口内的和小于target，i++
        如果大于或等于，那么先进行比较，sum-nums[left],然后left边界向右侧移动一位
        */
      var minSubArrayLen = function (target, nums) {
        let left = 0;
        let sum = 0;
        let min = Infinity;
        for (let i = 0; i < nums.length; i++) {
          sum += nums[i];
          while (sum >= target) {
            let tempLength = i - left + 1;
            min = Math.min(tempLength, min);
            sum -= nums[left];
            left++;
          }
        }
        return min == Infinity ? 0 : min;
      };
      minSubArrayLen(4, [1, 2, 3, 4, 5]);
    </script>
  </body>
</html>
